package com.dementors.base;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import butterknife.ButterKnife;

/**
 * Author  : itxuye(itxuye@gmail.com)|(http://itxuye.com)
 * Date    : 2015-11-30
 * Time    : 23:14
 * FIXME
 */
public abstract class BaseActivity extends AppCompatActivity
    implements View.OnClickListener {

  private long exitTime =0;

  @Override protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(provideContentViewId());

    ButterKnife.bind(this);
    if(savedInstanceState==null){
      initView();

      initData();

      setListener();
      getIntentData(savedInstanceState);
    }

  }

  /*
     提供布局ID
  */
  abstract protected int provideContentViewId();

  /*
    toolbar相关点击事件
   */
  public void onToolbarClick() {
  }

  /*
     * 初始化控件调用的方法，建议使用
     * 查找View
     *
     * @param id   控件的id
     * @param <T> View类型
     * @return
   */
  public <T extends View> T findById(@IdRes int id) {
    return (T) findViewById(id);
  }



  //初始化view
  protected abstract void initView();

  //设置监听
  protected abstract void setListener();

  //初始化data
  protected abstract void initData();

  protected abstract void getIntentData(Bundle saveInstance);

  public void doExitApp() {
    if ((System.currentTimeMillis() - exitTime) > 2000) {
      Toast.makeText(this, "再按一次，退出贝贝", Toast.LENGTH_SHORT).show();
      exitTime = System.currentTimeMillis();
    } else {
      finish();
    }
  }

  @Override public void onBackPressed() {
    doExitApp();
  }
}
